package ping.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import ping.download.HttpDownloader;
import ping.model.Mp3Info;
import ping.xml.Mp3ListContentHandler;
import android.os.Environment;

public class FileUtils {
	private String SDCardRoot;

	public FileUtils() {
		SDCardRoot = Environment.getExternalStorageDirectory()
				.getAbsolutePath()
				+ "/";
		System.out.println("SD dir:" + SDCardRoot);
	}

	public File createFileInSDCard(String fileName, String dir)
			throws IOException {
		File file = new File(SDCardRoot + dir + File.separator + fileName);
		file.createNewFile();
		return file;
	}

	public File createSDDir(String dir) {
		File dirFile = new File(SDCardRoot + dir + File.separator);
		System.out.println("creat dir:" + dirFile.mkdirs());
		return dirFile;
	}

	public boolean isFileExist(String fileName, String path) {
		File file = new File(SDCardRoot + path + File.separator + fileName);
		return file.exists();
	}

	public File write2SDFromInput(String path, String fileName,
			InputStream input) {
		File file = null;
		OutputStream output = null;
		try {
			createSDDir(path);
			file = createFileInSDCard(fileName, path);
			output = new FileOutputStream(file);
			byte buffer[] = new byte[4 * 1024];
			int temp;
			while ((temp = input.read(buffer)) != -1) {
				output.write(buffer, 0, temp);
			}
			output.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				output.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return file;
	}

	/**
	 * read floder of mp3 name and size.
	 */
	public List<Mp3Info> getMp3Files(String pth) {
		List<Mp3Info> mp3Infos = new ArrayList<Mp3Info>();
		File file = new File(SDCardRoot + File.separator + pth);
		File[] files = file.listFiles();		
		for (int i = 0; i < files.length; i++) {
			Mp3Info mp3Info = new Mp3Info();
			if (files[i].getName().endsWith("mp3")) {				
				mp3Info.setMp3Name(files[i].getName());
				mp3Info.setMp3Size(files[i].length() + "");
				String str = getLrcFileNamefromMp3File(files[i].getName());
				System.out.println("str:"+str);
				mp3Info.setLrcName("a1.lrc");
				mp3Infos.add(mp3Info);			
			} 			
		}
		return mp3Infos;
	}
	public String getLrcFileNamefromMp3File(String mp3FileName){
		HttpDownloader httpDownloader = new HttpDownloader();
		String result = httpDownloader.download(SDCardRoot + "mp3/resources.xml");
		List<Mp3Info> mp3Infos = parse(result);
		for (Iterator iterator = mp3Infos.iterator(); iterator.hasNext();) {
			Mp3Info mp3Info = (Mp3Info) iterator.next();
			if(mp3Info.getMp3Name().equals(mp3FileName)){
				return mp3Info.getLrcName();
			}
		}
		return null;
	}
	public List<Mp3Info> parse(String xmlStr){
		SAXParserFactory saxParserFactor = SAXParserFactory.newInstance();
		List<Mp3Info> infos = new ArrayList<Mp3Info>();
		try{
			XMLReader xmlReader = saxParserFactor.newSAXParser().getXMLReader();
			//
			Mp3ListContentHandler mp3ListContentHandler = new Mp3ListContentHandler(infos);
			xmlReader.setContentHandler(mp3ListContentHandler);
			xmlReader.parse(new InputSource(new StringReader(xmlStr)));
			for (Iterator iterator = infos.iterator(); iterator.hasNext();) {
				Mp3Info mp3Info = (Mp3Info) iterator.next();
				System.out.println(mp3Info);
			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		return infos;
	}
	
}
